package org.shock.ss.sms.commons.service.impl;

import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.shock.ss.sms.bean.Pager;
import org.shock.ss.sms.commons.db.ProxoolDbPool;
import org.shock.ss.sms.commons.db.TransactionManager;
import org.shock.ss.sms.commons.dao.BaseDao;
import org.shock.ss.sms.commons.dao.impl.BaseDaoImpl;
import org.shock.ss.sms.commons.service.BaseService;

public class BaseServiceImpl<T> implements BaseService<T> {
	protected BaseDao<T> dao = new BaseDaoImpl<T>();
	protected TransactionManager tx;
	protected Pager pager;
	@SuppressWarnings("unchecked")
	protected List list;
	@SuppressWarnings("unchecked")
	protected Map map;
	protected Logger log = Logger.getLogger(getClass());

	public BaseServiceImpl(BaseDao<T> dao) {
		super();
		this.dao = dao;
	}

	public void add(T bean) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			dao.add(bean);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
	}

	public void del(String sql) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			System.out.println("---1---------->");
			dao.del(sql);
			System.out.println("-------2------>");
			log.debug(sql);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
	}

	public Pager list(String dir, String sort, int pageno, int rownum, String sqlCount, String sql) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			pager = dao.list(dir, sort, pageno, rownum, sqlCount, sql);
			tx.commitAndClose();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
		return pager;
	}

	@SuppressWarnings("unchecked")
	public List listAll(String sql) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			list = dao.listAll(sql);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
		return list;
	}

	public void update(T bean) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			dao.update(bean);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}

	}

	@SuppressWarnings("unchecked")
	public Map view(String sql) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			map = dao.view(sql);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
		return map;
	}

	public void del(T bean) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			dao.del(bean);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
	}

	public T addBean(T bean) throws Exception {
		T result = null;
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			result = dao.addBean(bean);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
		return result;
	}

	public Integer addBatch(List<T> beanList) throws Exception {
		int result = 0;
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			result = dao.addBatch(beanList);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
		return result;
	}

	public boolean doSql(String sql) throws Exception {
		boolean result = false;
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			result = dao.doSql(sql);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
		return result;
	}

	public T get(Long id) throws Exception {
		T result = null;
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			result = dao.get(id);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null)
				tx.rollbackAndClose();
			throw e;
		}
		return result;
	}
}
